package com.yzt.zhuju.dialog;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yzt.zhuju.R;
import com.yzt.zhuju.gift.GiftSelectManager;
import com.yzt.zhuju.gift.adapter.GiftItemAdapter;
import com.yzt.zhuju.network.entity.gift.Gift;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.NonNull;

/**
 * author : bob
 * date   : 2023/1/14
 * desc   : 聊天页面礼物选择页
 */
public class ChatRoomGiftItemFragment extends Fragment implements GiftSelectManager.GiveGiftSelectChangedObservable {

    private int mType;

    private GiftItemAdapter giftItemAdapter;

    private TextView mPackNoDataTv;

    private List<Gift> dataList = new ArrayList<>();

    public static ChatRoomGiftItemFragment newInstance(int type, List<Gift> list) {
        ChatRoomGiftItemFragment fragment = new ChatRoomGiftItemFragment();
        fragment.mType = type;
        fragment.dataList = list;
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_item_give_gift, null);
        initView(rootView);
        return rootView;
    }

    public void initView(View view) {
        RecyclerView rvGiftList = view.findViewById(R.id.rv_gift_list);
        rvGiftList.setLayoutManager(new GridLayoutManager(getContext(), 4));
        giftItemAdapter = new GiftItemAdapter(getActivity(), mType);
        rvGiftList.setAdapter(giftItemAdapter);
        giftItemAdapter.initData(dataList);
        mPackNoDataTv = view.findViewById(R.id.tv_pack_no_data);
        noDataLayout(dataList.size() == 0);
        GiftSelectManager.getInstance().registerGiveGiftChangedObservable(this);
    }

    /**
     * 没有数据
     */
    public void noDataLayout(boolean noData) {
        mPackNoDataTv.setVisibility(noData ? View.VISIBLE : View.GONE);
        mPackNoDataTv.setTextColor(ContextCompat.getColor(getContext(), R.color.color_3b3d3f));
        Drawable drawable = ContextCompat.getDrawable(getContext(), R.mipmap.icon_pack_no_data);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mPackNoDataTv.setCompoundDrawables(null, drawable, null, null);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        GiftSelectManager.getInstance().unRegisterGiveGiftChangedObservable(this);
    }

    @Override
    public void selectGiftData(int type, Gift gift) {
        if (gift != null) {
            int index = -1;
            for (int i = 0; i < dataList.size(); i++) {
                dataList.get(i).setChoosed(false);
                if (gift.getGiftId() == dataList.get(i).getGiftId()) {
                    index = i;
                }
            }
            if (index >= 0) {
                dataList.get(index).setChoosed(true);
            }
            giftItemAdapter.initData(dataList);
        }

        if (selectGiftClickListener != null) {
            selectGiftClickListener.onSelect(type, gift);
        }
    }

    public interface OnSelectGiftClickListener {

        void onSelect(int type, Gift gift);
    }

    private OnSelectGiftClickListener selectGiftClickListener;

    public void setOnSelectGiftClickListener(OnSelectGiftClickListener listener) {
        this.selectGiftClickListener = listener;
    }
}